package demo.exception;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/8-08-08-16:33
 * @Description：demo.exception
 */
public class Return1 {
    public static void main(String[] args) {
        int i = new Return1().testReturn1();
        System.out.println(i);                //2
        System.out.println("--------------------------------");
        System.out.println(testReturn2());     //[1,3]
        System.out.println("---------------------------------");
        System.out.println(testReturn3());     //hello!
    }
    private int testReturn1() {
        int i = 1;
        try {
            i++;
            System.out.println("try:" + i);      //try:2
            return i;
        } finally {
            i++;
            System.out.println("finally:" + i);      //finally:3
        }
    }
    private static List<Integer> testReturn2() {
        List<Integer> list = new ArrayList<>();      //操作的是同一地址
        try {
            list.add(1);
            System.out.println("try:" + list); //try:[1]
            return list;
        }
        finally {
            list.add(3);
            System.out.println("finally:" + list);     //finally:[1,3]
        }
    }
    private static String testReturn3() {
        String a = "hello";         //String类 + 拼接是通过new新的字符串，地址更改，所以finally中和返回的操作的不是同一个地址
        try {
            a += "!";
            System.out.println("try:" + a);          //try:hello!
            return a;
        }
        finally {
            a += "world";
            System.out.println("finally:" + a);          //finally:hello!world
        }
    }
}
